package com.glory.good_dark.activity;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.good_dark.R;
import com.glory.good_dark.adapter.ProjectModelListAdapter;
import com.glory.good_dark.application.App;
import com.glory.good_dark.entity.ProjectModelEntity;
import com.glory.good_dark.entity.response.BaseEntity;
import com.glory.good_dark.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import butterknife.BindView;

public class ProjectModelActivity extends BaseProjectActivity {

    @BindView(R.id.tvEqpID)
    TextView tvEqpID;
    @BindView(R.id.rvProjectListView)
    RecyclerView rvProjectListView;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private ProjectModelListAdapter projectModelListAdapter;

    @Override
    protected void EQPReplaceSuccess(int tag, String content) {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_project_model;
    }

    @Override
    protected void initView() {
        tvEqpID.setText(App.EQPID);
        initRecyclerView(rvProjectListView,this);
        projectModelListAdapter = new ProjectModelListAdapter(null);
        projectModelListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        projectModelListAdapter.isFirstOnly(true);
        rvProjectListView.setAdapter(projectModelListAdapter);
    }

    @Override
    protected void initData() {
//        String data = "\"{\\\"data\\\":{\\\"BODY\\\":{\\\"INPUTDATAINFOLIST\\\":[{\\\"INPUTNAME\\\":\\\"清理设备数据\\\",\\\"DATATYPE\\\":\\\"1\\\",\\\"DATAVALUE\\\":null},{\\\"INPUTNAME\\\":\\\"清理物料数据\\\",\\\"DATATYPE\\\":\\\"1\\\",\\\"DATAVALUE\\\":null},{\\\"INPUTNAME\\\":\\\"清理批次数据\\\",\\\"DATATYPE\\\":\\\"1\\\",\\\"DATAVALUE\\\":null},{\\\"INPUTNAME\\\":\\\"清理点检数据\\\",\\\"DATATYPE\\\":\\\"1\\\",\\\"DATAVALUE\\\":null}],\\\"EQP_ID\\\":\\\"WB170\\\",\\\"OPERATOR\\\":\\\"\\\"},\\\"HEADER\\\":{\\\"MACHINENAME\\\":\\\"Android SDK built for x86\\\",\\\"MESSAGENAME\\\":\\\"PDAEngineeringModeInfo\\\",\\\"TRANSACTIONID\\\":\\\"9ab5b299-5d03-4140-ac43-374a3a61631e\\\",\\\"EVENTTIME\\\":\\\"2020-07-31 09:07:30\\\"},\\\"RETURN\\\":{\\\"RETURNCODE\\\":0,\\\"RETURNMESSAGE\\\":\\\"设备：WB170获取工程模式数据成功\\\"}}}\"";
//        BaseEntity<ProjectModelEntity> bean = (new Gson()).fromJson(data, new TypeToken<BaseEntity<ProjectModelEntity>>() {}.getType());
//        if (bean.isSuccess(getApplicationContext(),getSupportFragmentManager())) {
//            projectModelListAdapter.setNewData(bean.getData().getBody().getINPUTDATAINFOLIST());
//        }
        FreeUI_EntityCallBack<BaseEntity<ProjectModelEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<ProjectModelEntity>>
                (new TypeToken<BaseEntity<ProjectModelEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<ProjectModelEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                    projectModelListAdapter.setNewData(response.body().getData().getBody().getINPUTDATAINFOLIST());
                }

            }

            @Override
            public void onError
                    (Response<BaseEntity<ProjectModelEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDAEngineeringModeInfo");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("EQP_ID",App.EQPID);
        map.addProperty("OPERATOR",App.userEntity.getWORKNO());
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA",this,callBack,"PDAEngineeringModeInfo",map);
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("工程模式");
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FreeUI_EntityCallBack<BaseEntity<ProjectModelEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<ProjectModelEntity>>
                        (new TypeToken<BaseEntity<ProjectModelEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess
                            (final Response<BaseEntity<ProjectModelEntity>> response) {
                        if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                            showConfirmDialog(ProjectModelActivity.this,"提交成功！");
                        }

                    }

                    @Override
                    public void onError
                            (Response<BaseEntity<ProjectModelEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "PDAEngineeringModeInfo");
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("EQP_ID",App.EQPID);
                map.addProperty("OPERATOR",App.userEntity.getWORKNO());
                map.add("INPUTDATAINFOLIST",new JsonParser().parse(new Gson().toJson(projectModelListAdapter.getData())).getAsJsonArray());
                NetUtils.requestPostNet(App.WEB_URL + "/api/PDA",ProjectModelActivity.this,callBack,"PDAEngineeringModeSubmit",map);
            }
        });
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }
}